<extend name="common:commonSimpleNewTemplate" />
<block name="main_content">
<div id="">

	<div class="container-fluid">
		<div class="row-fluid">
			<div class="span12">
				<div class="widget-box">
					<div class="widget-title">
						<span class="icon"> <i class="icon-info-sign"></i>
						</span>
						<h5>经销商信息</h5>
					</div>
					<div class="widget-content nopadding">
						<form novalidate="novalidate" id="basic_validate"
							name="basic_validate" action="#" method="post"
							class="form-horizontal">
							<div class="control-group">
								<label class="control-label">经销商名称:</label>
								<div class="controls">
									<input type="text" id="dealer_name" name="dealer_name"/>
									<input type="hidden" id="sale_id"  value="{$sale_id}" >
								</div>
							</div>
							<div class="control-group">
								<label class="control-label">经销商类型：</label>
								<div class="controls">
								<select id="dealer_type" name="dealer_type" style="width:220px;" >
									<option value="">--ALL--</option>
									<option value="1">一般纳税人</option>
									<option value="2">个体工商户</option>								
								</select>
								</div>
							</div>
							<div class="control-group">
								<label class="control-label">业务对接人:</label>
								<div class="controls">
									<input type="text" id="business_leader" name="business_leader"/>
								</div>
							 </div>
							 <div class="control-group">
								<label class="control-label">大区:</label>
								<div class="controls">
									<input type="text" id="area_name" name="area_name"/>
								</div>
							 </div>
							<div class="control-group">
								<label class="control-label">签约状态:</label>
								<div class="controls">
									<select id="sign_status" name="sign_status" style="width:220px;">
					                  <option value="">--ALL--</option>
					                  <option value="1">待签约</option>
					                  <option value="2">经销商待盖章</option>
					                  <option value="3">已签约</option>
					                </select>
								</div>
							</div>
							<div class="control-group">
								<label class="control-label">状态:</label>
								<div class="controls">
									 <select id="status" name="status" style="width:220px;">
									 <option value="">--ALL--</option>
					                  <option value="1">待审核</option>
					                  <option value="2">已审核</option>
					                   <option value="3">审核拒绝</option>
					                </select>
								</div>
							</div>
							<div class="form-actions">
								<input id="btn_dealer_search" type="button" class="btn btn-success" value="搜索">
								
								
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>
	
	<div class="row-fluid">
      <div class="span12">
        <div class="widget-box">
          <div class="widget-title"> <span class="icon"> <i class="icon-th"></i> </span>
            <h5>搜索结果</h5>
          </div>
          <div class="widget-content nopadding">
            <table id="search_result"  class="table table-bordered table-striped">
              <thead>
                <tr>
                  <th>操作</th>
                  <th>经销商名称</th>
                  <th>经销商类型</th>
                  <th>经销商的默认折扣返点</th>
                  <th>业务对接人</th>
                  <th>大区</th>
                  <th>注册资金</th>
                  <th>法人代表</th>
                  <th>税号</th>
                  <th>财务联系人</th>
                  <th>发票寄送地址</th>
                  <th>签约状态</th>
                  <th>签约时间</th>
                  <th>状态</th>
                  
                </tr>
              </thead>
              <tbody>
              </tbody>
            </table>
          </div>
          
       <button id='check_these_dealers' class="btn btn-success" type="submit">确认</button>
      </div>
      <div id="paging"
                         class="fg-toolbar ui-toolbar ui-widget-header ui-corner-bl ui-corner-br ui-helper-clearfix">
                        <div class="dataTables_paginate fg-buttonset ui-buttonset fg-buttonset-multi ui-buttonset-multi paging_full_numbers"
                             id="DataTables_Table_0_paginate">
                            <a page_index="1"
                               class="first ui-corner-tl ui-corner-bl fg-button ui-button ui-state-default" tabindex="0"
                               id="DataTables_Table_0_first">首页</a>
                            <a class="previous fg-button ui-button ui-state-default" tabindex="0"
                               id="DataTables_Table_0_previous">上页</a>
                            <span>共<span id="total_count">0</span>条 <span id="curr_page_index">0</span>/<span
                                    id="total_page">0</span></span>
                            <a class="next fg-button ui-button ui-state-default" tabindex="0"
                               id="DataTables_Table_0_next">下页</a>
                            <a class="last ui-corner-tr ui-corner-br fg-button ui-button ui-state-default" tabindex="0"
                               id="DataTables_Table_0_last">末页</a>
       			<span>
        			<span><input type="text" id="page_input" style="width:20px;" class="ui-state-default"></span>

        			<a class="fg-button ui-button ui-state-default" id="page_jump" tabindex="0">跳转</a>
       			</span>
                        </div>
       </div>
    </div>
  </div>

</div>
</block>

<block name="js_content">
<script>
DealerInfoPage = {
		'BindEvent': function(){
			
			$('#check_these_dealers').bind('click',function(){
				
				var sale_id = $('#sale_id').val();
				
				var length = $('input[name="multidealers"]:checked').length;
				
				if(length == 0){					
				alert("请选择一个经销商添加!");
				return;	
				}
				
				$('input[name="multidealers"]:checked').each(function(){ 
					
					var dealer_id = $(this).val(); 				
				    DealerInfoPage.adddealersalerelationship(sale_id,dealer_id);
				   
				}); 
				
			  parent.DealerInfoPage.Search(1);
			  var index = parent.layer.getFrameIndex(window.name); //获取当前窗体索引
			  parent.layer.close(index); //执行关闭
				
				
			});
			
			
			
			
			$('#btn_dealer_search').bind('click', function(){
				
				 
				 var page_index = 1;
				 DealerInfoPage.Search(page_index);
			});
			

			$('#page_jump').bind('click', function(){
				
				 var page_index = $('#page_input').val();
				 var total_page = $('#total_page').text();
				
				 if(page_index <= 0 || page_index > total_page  ){
					 page_index =1;
				 }
				 
				 DealerInfoPage.Search(page_index);
			});
			
			
            $(".first,.previous,.last,.next").bind('click', function () {
                if ($(this).hasClass('ui-state-disabled')) {
                    return false;
                }

                var page_index = $(this).attr('page_index');
                if (page_index == undefined) {
                    page_index = 1;
                }
                //page_index = 1;
                DealerInfoPage.Search(page_index);
            });
		},
		
		'adddealersalerelationship':function(sale_id,dealer_id){
			
			 condition = "sale_id="+sale_id+"&dealer_id="+dealer_id;
			   $.ajax({
	                type: 'post',
	                url: '/AjaxService/center/sale/adddealersalerelationship',
	                data: condition,
	                dataType: 'json',
	                async: false,
	                success: function (data) { 

	                },
	                error: function (e, err_name, err_text) {
	                    alert(err_text);
	                }
	            });
			   
			   
			
		},
		
		
		'bindcheckbox':function(checkbox){
			
			   var sale_id = $('#sale_id').val();
			   var dealer_id =  $(checkbox).val();
			   condition = "sale_id="+sale_id+"&dealer_id="+dealer_id;
			   $.ajax({
	                type: 'post',
	                url: '/AjaxService/center/sale/querydealersalerelationship',
	                data: condition,
	                dataType: 'json',
	                async: false,
	                success: function (data) {
	                	if(data.Result!=null){
	                		
	                		if(data.Result['dealer_id']==dealer_id);
	                		 $(checkbox).attr('checked',false);
	                		 $(checkbox).attr('disabled',true);
	                		 
	                	}
                      

	                },
	                error: function (e, err_name, err_text) {
	                    alert(err_text);
	                }
	            });
			   
			   
					
		
			
		},
   
		'Search': function (page_index) {
            var condition = $("#basic_validate").serialize();
            condition += "&pageSize=20&index="+page_index+"&dealer_channel=3";

            $.ajax({
                type: 'post',
                url: '/AjaxService/center/sale/querydealerwithoutrelationship',
                data: condition,
                dataType: 'json',
                async: false,
                success: function (data) {
                	DealerInfoPage.BindTable(data.Result, page_index);
                	DealerInfoPage.SetPageIndex(data.Result.totalPage, data.Result.totalCount, page_index);


                },
                error: function (e, err_name, err_text) {
                    alert(err_text);
                }
            });

        },
        
		'BindTable': function(result,page_index){
			lst_dealer = result.datas;
			if(lst_dealer.length == 0){				
				$('#search_result tbody').html('');
				var msg ="<tr><td colspan='14' style='text-align:center'>暂无数据!</td></tr>";				
				$('#search_result tbody').append(msg);	
				return;							
			};
			var html = '';
			for(var index in lst_dealer){
				var dealer_en = lst_dealer[index];
				var tr_html = "<tr>";
				tr_html += "<td> <input class='input_check_box' onclick='DealerInfoPage.bindcheckbox(this)' name='multidealers' value='"+dealer_en.dealer_id+"' type='checkbox' style='position:relative;width: 50px; opacity: 100;'></td>"
				tr_html += "<td>" + dealer_en.dealer_name+"</td>";
				tr_html += "<td>" + DealerInfoPage.ConvertDealerType(dealer_en.dealer_type)+"</td>";
				tr_html += "<td>" + dealer_en.discount_rate + "</td>"
				tr_html += "<td>" + dealer_en.business_leader + "</td>"
				tr_html += "<td>" + dealer_en.area_name + "</td>"
				tr_html += "<td>" + dealer_en.registered_capital+"</td>";
				tr_html += "<td>" + dealer_en.leal_person+"</td>";
				tr_html += "<td>" + dealer_en.tax_no + "</td>"
				tr_html += "<td>" + dealer_en.finance_leader + "</td>"
				tr_html += "<td>" + dealer_en.invoice_address + "</td>"
				tr_html += "<td>" + DealerInfoPage.ConvertSignStatus(dealer_en.sign_status)+"</td>";
				tr_html += "<td>" + DealerInfoPage.ConvertStrNull(dealer_en.sign_date)+"</td>";
				tr_html += "<td>" + DealerInfoPage.ConvertStatus(dealer_en.status) + "</td>"
				
				
				tr_html += "</tr>";
				html += tr_html;
			} 
			$('#search_result tbody').html('');
			$('#search_result tbody').append(html);
			$('#search_result').attr('page_index', page_index);

		},
		
		'SetPageIndex': function (total_page, total_count, page_index) {
            $('#total_count').text(total_count);
            $('#total_page').text(total_page);
            $('#curr_page_index').text(page_index);
            page_index = parseInt(page_index);

            if (page_index == 1) {
                $('#paging .first').addClass('ui-state-disabled');
                $('#paging .previous').addClass('ui-state-disabled');
                $('#paging .first').attr('page_index', 1);
                $('#paging .previous').attr('page_index', 1);
            }
            else {
                $('#paging .first').removeClass('ui-state-disabled');
                $('#paging .previous').removeClass('ui-state-disabled');
                $('#paging .first').attr('page_index', 1);
                $('#paging .previous').attr('page_index', page_index - 1);
            }

            if (total_page == page_index) {
                $('#paging .last').addClass('ui-state-disabled');
                $('#paging .next').addClass('ui-state-disabled');
                $('#paging .last').attr('page_index', total_page);
                $('#paging .next').attr('page_index', total_page);
            }
            else {
                $('#paging .last').removeClass('ui-state-disabled');
                $('#paging .next').removeClass('ui-state-disabled');
                $('#paging .last').attr('page_index', total_page);
                $('#paging .next').attr('page_index', page_index + 1);
            }

        },

		'ConvertStrNull':function(obj){
			
			if(obj == null){
				return '';
			}
			else{
				return obj;
			}
		},
		'ConvertSignStatus':function(obj){
			if(obj == '1'){
				return '待签约';
			}else if(obj =='2'){
				return '经销商待盖章';
			}else if(obj =='3'){
				return '已签约';
			}
		} ,		
		'ConvertStatus':function(obj){
			if(obj == '1'){
				return '待审核';
			}else if(obj =='2'){
				return '已审核';
			}else if(obj =='3'){
				return '审核拒绝';
			}
		} ,		
		
		'ConvertDealerType':function(obj){
			if(obj == '1'){
				return '一般纳税人';
			}else if(obj =='2'){
				return '个体工商户';
			}
		} ,		
		
};


$(document).ready(function(){
	DealerInfoPage.BindEvent();
});


</script>
</block>